﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using AjaxControlToolkit;

namespace VINETLIFE.AdminCP
{
    public partial class ListCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowData();
                if (Request.QueryString["Id"] != null)
                    details.Visible = true;
            }
        }

        cmsCustomerBL ItemBL = new cmsCustomerBL();
        private void ShowData()
        {
            if (dropSort.SelectedValue == "1")
            {
                gridView.DataSource = ItemBL.GetByIsVangLai(false);
                gridView.DataBind();
                details.Visible = false;
            }
            else if (dropSort.SelectedValue == "2")
            {
                gridView.DataSource = ItemBL.GetByIsVangLai(true);
                gridView.DataBind();
                details.Visible = false;
            }

        }

        protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            ShowData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            lbThongBao.Text = "";
            try
            {
                lbThongBao.Text = "";
                gridView.PageIndex = e.NewPageIndex;
                ShowData();
            }
            catch { }
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //try
            //{
            //    lbThongBao.Text = "";
            //    int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
            //    cmsCustomerDO itemDO = new cmsCustomerDO();
            //    itemDO.Id = id;
            //    ItemBL.Delete(itemDO);
            //    //ResetForm();
            //    ShowData();
            //}
            //catch (Exception ex)
            //{
            //    Myfunction.ThongBao(lbThongBao, ex.Message);
            //}
        }
        protected void btnXoaChon_Click(object sender, EventArgs e)
        {
            try
            {
                lbThongBao.Text = "";
                foreach (GridViewRow item in gridView.Rows)
                {
                    foreach (Control ctrl in item.Cells[1].Controls)
                    {
                        if (ctrl.GetType() == typeof(CheckBox))
                        {
                            CheckBox check = (CheckBox)ctrl;
                            if (check.Checked)
                            {
                                int id = Convert.ToInt32(item.Cells[2].Text);
                                cmsCustomerDO itemDO = new cmsCustomerDO();
                                itemDO.Id = id;
                                ItemBL.Delete(itemDO);
                            }
                            break;
                        }
                    }
                }
                //ResetForm();
                ShowData();
            }
            catch (Exception ex)
            {
                Myfunction.ThongBao(lbThongBao, ex.Message);
            }
        }
        protected void linkCheckAll_Click(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        check.Checked = true;
                        break;
                    }
                }
            }
        }
        protected void linkUnCheckAll_Click(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        check.Checked = false;
                        break;
                    }
                }
            }
        }
        protected void dropIsView_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            ShowData();
        }
        protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            ShowData();
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                bool isRead = Convert.ToBoolean(e.Row.Cells[6].Text);
                if (isRead) e.Row.Cells[6].Text = "Đã đọc";
                else
                {
                    e.Row.Cells[6].Text = "Chưa đọc";
                    e.Row.ForeColor = System.Drawing.Color.Red;
                }

                e.Row.Cells[5].Text = Convert.ToDateTime(e.Row.Cells[5].Text).ToShortDateString();
            }
            catch
            {

            }
        }
        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                cmsCustomerDO itemCustomerDO = new cmsCustomerDO();
                cmsCustomerBL itemCustomerBL = new cmsCustomerBL();
                if (e.CommandName == "View")
                {
                    lbThongBao.Text = "";
                    int index = Convert.ToInt32(e.CommandArgument);
                    int customerId = int.Parse(e.CommandArgument.ToString());
                    Session["CustomerID"] = customerId;

                    itemCustomerDO.Id = customerId;
                    itemCustomerDO = itemCustomerBL.Select(itemCustomerDO);

                    ltAddress.Text = itemCustomerDO.Address;
                    ltBrithday.Text = itemCustomerDO.Brithday.ToString("dd/MM/yyyy");
                    ltDayregister.Text = itemCustomerDO.RegisterDay.ToString("dd/MM/yyyy");
                    ltFullname.Text = itemCustomerDO.FullName;
                    ltPhone.Text = itemCustomerDO.Phone;
                    ltCustomerCode.Text = itemCustomerDO.CustomerCode;
                    ltCMTND.Text = itemCustomerDO.Identification;
                    if (itemCustomerDO.Sex == true) radioNam.Checked = true;
                    else RadioButton1.Checked = true;
                    if (itemCustomerDO.IsEnable == true) RadioBat.Checked = true;
                    else RadioTat.Checked = true;
                    ltUsername.Text = itemCustomerDO.UserName;
                    if (itemCustomerDO.IsVangLai == true) rdoVanglai.Checked = true;
                    else
                    {
                        rdoThanhvien.Checked = true;
                        rdoVanglai.Checked = false;
                    }
                    ltEmail.Text = itemCustomerDO.Email;
                    //contactData.Read(id);
                    details.Visible = true;

                    ltFullname.Focus();
                }
                else if (e.CommandName == "Delete")
                {
                    int id = int.Parse(e.CommandArgument.ToString());
                    itemCustomerDO.Id = id;
                    itemCustomerDO = itemCustomerBL.Select(itemCustomerDO);
                    itemCustomerBL.Delete(itemCustomerDO);
                    ShowData();
                }
            }
            catch
            {

            }
        }

        //void LoadDetail(int OrderID)
        //{
        //    listDetail.DataSource = new cmsOrderDetailsBL().GetByOrderId(OrderID);
        //    listDetail.DataBind();
        //}

        protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbThongBao.Text = "";
            gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
            ShowData();
        }

        protected void gridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(gridView.DataKeys[gridView.SelectedIndex].Value.ToString());
            Response.Redirect("Product.aspx?ProductID=" + id.ToString());
        }

        void Update()
        {
            string username = ltUsername.Text;
            cmsCustomerDO customerDO = new cmsCustomerDO();
            cmsCustomerBL customerBL = new cmsCustomerBL();
            customerDO.UserName = username;
            customerDO = customerBL.SelectByUsername(customerDO);
            customerDO.Id = Convert.ToInt32(Session["CustomerID"].ToString());
            customerDO.Address = ltAddress.Text;
            customerDO.Brithday = Convert.ToDateTime(ltBrithday.Text);
            customerDO.Email = ltEmail.Text;
            customerDO.FullName = ltFullname.Text;
            if (RadioBat.Checked) customerDO.IsEnable = true;
            if (RadioTat.Checked) customerDO.IsEnable = false;
            if (rdoVanglai.Checked)
                customerDO.IsVangLai = true;
            else
                customerDO.IsVangLai = false;
            customerDO.Password = Ultility.EncryptMd5(ltPassword.Text);
            customerDO.Phone = ltPhone.Text;
            customerDO.RegisterDay = Convert.ToDateTime(ltDayregister.Text);
            if (customerDO.CreatedDate < DateTime.MinValue)
                customerDO.CreatedDate = DateTime.Now;
            customerDO.LastSaveDate = DateTime.Now;
            customerDO.LastModifiedById = int.Parse(Session["UserID"].ToString());
            if (radioNam.Checked) customerDO.Sex = true;
            if (RadioButton1.Checked) customerDO.Sex = false;

            customerBL.Update(customerDO);
            details.Visible = false;

            Myfunction.ThongBao(lbThongBao, "Cập nhật thành công");
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            ShowData();
        }

        protected void btnCapnhat_Click(object sender, EventArgs e)
        {
            Update();
            ShowData();
        }
    }
}